package com.aos.eap.activity;


import android.os.Handler;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aos.eap.R;
import com.aos.eap.adapter.CheckListAdapter;
import com.aos.eap.base.BaseDialogActivity;
import com.aos.eap.base.Constant;
import com.aos.eap.bean.CheckBean;
import com.aos.eap.bean.EqpPmBean;
import com.aos.eap.bean.PmtypeBean;
import com.aos.eap.bean.net.BaseResponseBean;
import com.aos.eap.utils.MyUtils;
import com.aos.eap.utils.NetUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

public class EE_ChangeEqpStatusActivity extends BaseDialogActivity {


    @BindView(R.id.tvUser)
    TextView tvUser;
    @BindView(R.id.etEqpId)
    FreeUI_ClearEditText etEqpId;
    @BindView(R.id.tvMainStatus)
    TextView tvMainStatus;
    @BindView(R.id.tvChildStatus)
    TextView tvChildStatus;
    @BindView(R.id.tvControlStatus)
    TextView tvControlStatus;
    @BindView(R.id.tvType)
    TextView tvType;
    @BindView(R.id.ivTypeArrow)
    ImageView ivTypeArrow;
    @BindView(R.id.flType)
    FrameLayout flType;
    @BindView(R.id.etRemark)
    FreeUI_ClearEditText etRemark;
    @BindView(R.id.rvInputList)
    RecyclerView rvInputList;
    @BindView(R.id.llCheck)
    LinearLayout llCheck;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    private EqpPmBean eqpPmBean;
    private CheckListAdapter checkListAdapter;
    private FreeUI_GeneralPop typePop;
    private List<String> pmTypeList;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_ee_change_eqp_status;
    }

    @Override
    protected void initView() {
        tvUser.setText(Constant.CURRENT_USER);
        rvInputList.setLayoutManager(new LinearLayoutManager(EE_ChangeEqpStatusActivity.this));
        rvInputList.setItemAnimator(new DefaultItemAnimator());
        rvInputList.addItemDecoration(new DividerItemDecoration(EE_ChangeEqpStatusActivity.this, 1));

        checkListAdapter = new CheckListAdapter(null);
        checkListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        checkListAdapter.isFirstOnly(true);
        rvInputList.setAdapter(checkListAdapter);
        MyUtils.setEditTextListener(etEqpId, this, s -> requestInfoByEqpId(s));
    }

    private void requestInfoByEqpId(String eqpId) {
        JsonObject map = new JsonObject();
        map.addProperty("EqpID", eqpId);
        NetUtils.requestGetNet(this, "EMS_EqpPmLoad", Constant.REQUEST_URL + "/api/AosPms/", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<EqpPmBean>>(
                new TypeToken<BaseResponseBean<EqpPmBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<EqpPmBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    eqpPmBean = response.body().getResponse().getBody();
                    setPageData(eqpPmBean);
                } else {
                    eqpPmBean = null;
                    setPageData(new EqpPmBean());
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<EqpPmBean>> response) {
                super.onError(response);
                eqpPmBean = null;
                setPageData(new EqpPmBean());
                uploadLog(response.getException());
                loadError(response.getException(), "EMS_EqpPmLoad");
            }
        });
    }

    private void setPageData(EqpPmBean eqpPmBean) {
        tvMainStatus.setText(eqpPmBean.getMainMode());
        tvChildStatus.setText(eqpPmBean.getSubMode());
        tvControlStatus.setText(eqpPmBean.getCtrlMode());
        etRemark.setText(eqpPmBean.getRemark());
        pmTypeList = new ArrayList<>();
        for (PmtypeBean entity : eqpPmBean.getEqppmtypelist()) {
            pmTypeList.add(entity.getPmtypelist());
        }
        checkListAdapter.setNewData(eqpPmBean.getCheckList());
        if (pmTypeList.size() != 0) {
            tvType.setText(pmTypeList.get(0));
        } else {
            tvType.setText("");
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("设备PM");
        freeUI_titleView.setLeftListener(view -> showBackDialog(EE_ChangeEqpStatusActivity.this));
        freeUI_titleView.setRightListener("提交", v -> {
            if (MyUtils.isFastClick()){
                showToast("您点击的太快了",false);
                return;
            }
            if (eqpPmBean == null) {
                showToast("未获取到页面信息，请重新扫描设备编号", false);
                etEqpId.setText("");
                MyUtils.setEditTextFocus(etEqpId);
                return;
            }
//            if (!eqpPmBean.getSubMode().equalsIgnoreCase("IDLE")) {
//                showToast("子状态不为IDLE，不能提交", false);
//                etEqpId.requestFocus();
//                return;
//            }
            String type = tvType.getText().toString().trim();
            if (FreeApi_StringUtils.isEmpty(type)) {
                showToast("请选择PM类型", false);
                return;
            }
            boolean isOk = true;
            for (CheckBean checkBean : checkListAdapter.getData()) {
                if (checkBean.getItemValue().length() == 0) {
                    isOk = false;
                    break;
                }
            }
            if (!isOk) {
                showToast("请填写所有检查项", false);
                return;
            }
            JsonObject map = new JsonObject();
            map.addProperty("EqpID", eqpPmBean.getEqpID());
            map.addProperty("MainMode", eqpPmBean.getMainMode());
            map.addProperty("SubMode", eqpPmBean.getSubMode());
            map.addProperty("CtrlMode", eqpPmBean.getCtrlMode());
            map.addProperty("PmType", type);
            map.addProperty("Remark", etRemark.getText().toString().trim());
            map.add("CheckList", new JsonParser().parse(new Gson().toJson(checkListAdapter.getData())).getAsJsonArray());
            requestSubmit(map);
        });
        return new FreeUI_AddViewEntity("header", llMain);
    }

    private void requestSubmit(JsonObject map) {
        NetUtils.requestGetNet(this, "EMS_EqpPm", Constant.REQUEST_URL + "/api/AosPms/", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<?>>(
                new TypeToken<BaseResponseBean<?>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<?>> response) {
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    showToast("提交成功", true);
                    new Handler().postDelayed(EE_ChangeEqpStatusActivity.this::finish, 2000);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<?>> response) {
                super.onError(response);
                uploadLog(response.getException());
                loadError(response.getException(), "EMS_EqpPm");
            }
        });
    }

    @OnClick(R.id.flType)
    public void onViewClicked() {
        if (MyUtils.isFastClick()){
            showToast("您点击的太快了",false);
            return;
        }
        if (typePop != null) {
            if (typePop.isShowing()) {
                typePop.dismiss();
            } else {
                FreeApi_PopUtils.setArrowShow(ivTypeArrow);
                typePop.showAsDropDown(flType, 0, 0);
            }
        } else {
            if (pmTypeList == null)
                return;
            typePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flType.getWidth(), pmTypeList,
                    ivTypeArrow, (s, i) -> {
                        tvType.setText(s);
                    });
            FreeApi_PopUtils.setArrowShow(ivTypeArrow);
            typePop.showAsDropDown(flType, 0, 0);
        }
    }
}
